
import 'package:flutter/material.dart';

class SnackBarTestPage extends StatefulWidget {
  const SnackBarTestPage({super.key});

  @override
  State<SnackBarTestPage> createState() => _SnackBarTestPageState();
}

class _SnackBarTestPageState extends State<SnackBarTestPage> {
  
  bool offstage = true;
  GlobalKey key = GlobalKey();

  Size _getSizedBoxSize() {
    final RenderBox renderBox =
        key.currentContext!.findRenderObject()! as RenderBox;
    return renderBox.size;
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("下载APK并安装")),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Offstage(
              offstage: offstage,
              child: SizedBox(
                key: key,
                width: 150.0,
                height: 150.0,
                child: Container(
                  color: Colors.red,
                ),
              ),
            ),
            ElevatedButton(
              child: const Text('切换offstage'),
              onPressed: () {
                setState(() {
                  offstage = !offstage;
                });
              },
            ),
            ElevatedButton(
              child: const Text('检测SizedBox大小'),
              onPressed: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(
                    content:
                    Text('SizedBox is ${_getSizedBoxSize()}'),
                  ),
                );
              }
            )
          ]
        ),
      ),
    );
  }
}